﻿using System.IO;

public abstract class FieldBase
{
    private static string IndentStr = "    ";

    protected string _className;
    protected string _desc;
    protected string _type;
    protected string _name;
    public string F_ClassName { get { return _className; } }
    public virtual string F_Desc { get { return _desc; } }
    public virtual string F_Type { get { return _type; } }
    public virtual string F_Name { get { return _name; } }


    public FieldBase(string className, string desc, string type, string name)
    {
        _className = className;
        _desc = desc;
        _type = type;
        _name = name;
    }

    /// <summary>
    /// 获取缩进等级
    /// </summary>
    public static string GetIndent(int indentLevel)
    {
        string indentStr = string.Empty;

        for (int i = 0; i < indentLevel; ++i)
        {
            indentStr += IndentStr;
        }

        return indentStr;
    }

    /// <summary>
    /// 写入字段代码
    /// </summary>
    public virtual void WriteField(int indentLevel, StreamWriter SW) { SW.WriteLine(GetIndent(indentLevel) + "[SerializeField] private {0} _{1}; // {2}".SafeFormat(F_Type, F_Name, F_Desc)); }

    /// <summary>
    /// 写入属性代码
    /// </summary>
    public virtual void WriteProperty(int indentLevel, StreamWriter SW)
    {
        SW.WriteLine(GetIndent(indentLevel) + "/// <summary> {0} </summary>".SafeFormat(_desc));
        SW.WriteLine(GetIndent(indentLevel) + "public {0} {1} {{ get {{ return _{2}; }}}}".SafeFormat(F_Type, F_Name, F_Name));
        SW.WriteLine(string.Empty);
    }

    /// <summary>
    /// 写入构造参数代码
    /// </summary>
    public virtual void WriteParameter(int indentLevel, StreamWriter SW, bool isEnd)
    {
        string str = isEnd ? "{0} l{1} // {2}" : "{0} l{1}, // {2}";
        SW.WriteLine(GetIndent(indentLevel) + str.SafeFormat(F_Type, F_Name, F_Desc));
    }

    /// <summary>
    /// 写入构造函数内应用代码
    /// </summary>
    public virtual void WriteConstructor(int indentLevel, StreamWriter SW)
    {
        SW.WriteLine(string.Format(GetIndent(indentLevel) + "l{0}Data._{1} = l{2};".SafeFormat(F_ClassName, F_Name, F_Name)));
    }

    /// <summary>
    /// 导出输出信息
    /// </summary>
    public virtual string ExportDataStr()
    {
        return "(data.{0}).ToString()".SafeFormat(F_Name);
    }

    /// <summary>
    /// 写入Enum
    /// </summary>
    public virtual void WriteEnum(int indentLevel, StreamWriter SW) { }

    /// <summary>
    /// 写入GUI
    /// </summary>
    public abstract void WriteGUI(int indentLevel, StreamWriter SW);

    /// <summary>
    /// 写入获取数据代码
    /// </summary>
    public abstract void WriteGetData(int indentLevel, StreamWriter SW, bool isEnd);
}
